package com.lins.storedisk.utils;

import javax.servlet.http.HttpSession;
import java.util.HashMap;

public class MySessionUtils {

    //实例
    private static MySessionUtils instance;
    private HashMap sessionMap;

    private MySessionUtils() {
        sessionMap = new HashMap();
    }

    //获取上下文实例，也就是本对象自己
    public static MySessionUtils getInstance() {
        if (instance == null) instance = new MySessionUtils();
        return instance;
    }

    //把session存起来
    public synchronized void addSession(HttpSession session) {
        if (session != null) sessionMap.put(session.getId(), session);
    }
    public synchronized void addSession(Integer uid,HttpSession session) {
        if (session != null) sessionMap.put(uid, session);
    }


    //删除session
    public synchronized void delSession(HttpSession session) {
        if (session != null) {
            sessionMap.remove(session.getId());
        }
    }
    public synchronized void delSession(Integer uid,HttpSession session) {
        if (session != null) {
            sessionMap.remove(uid);
        }
    }

    //根据session_id获取session
    public synchronized HttpSession getSession(String session_id) {
        if (session_id == null)
            return null;
        return (HttpSession) sessionMap.get(session_id);
    }

    //根据uid获取session
    public synchronized HttpSession getSession(Integer uid) {
        if (uid == null) return null;
        return (HttpSession) sessionMap.get(uid);
    }
}

